package com.hxkj.service.impl;


import com.hxkj.client.service.FriendlyLinkServiceClient;
import com.hxkj.domain.enums.FriendlyLinkExceptionEnum;
import com.hxkj.domain.po.FriendlyLink;
import com.hxkj.service.FriendlyLinkService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户mapper
 * <p>
 * Created by chenfangzhou on 2018/5/15.
 */
@Service
@Slf4j
public class FriendlyLinkServiceImpl implements FriendlyLinkService {


    @Autowired
    private FriendlyLinkServiceClient friendlyLinkServiceClient;


    /**
     * 保存友情链接
     * @param friendlyLink
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct saveFriendlyLink(FriendlyLink friendlyLink) throws Exception {
        if (friendlyLink == null) {
            throw new CustomException(FriendlyLinkExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            // 查询站点列表
            result = friendlyLinkServiceClient.saveFriendlyLink(friendlyLink);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(FriendlyLinkExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(FriendlyLinkExceptionEnum.getEnumByCode(result.getCode()));

        }
    }

    /**
     * 查询单条友情链接
     * @param friendlyLink
     * @return
     * @throws Exception
     */
    @Override
    public ResultDataStruct<FriendlyLink> getFriendlyLink(FriendlyLink friendlyLink) throws Exception {
        if (friendlyLink == null) {
            throw new CustomException(FriendlyLinkExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultDataStruct<FriendlyLink> result = new ResultDataStruct();
        try {
            result = friendlyLinkServiceClient.getFriendlyLink(friendlyLink);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(FriendlyLinkExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(FriendlyLinkExceptionEnum.getEnumByCode(result.getCode()));
        }
    }

    /**
     * 查询友情链接集合
     * @param friendlyLink
     * @param pageParam
     * @return
     * @throws Exception
     */
    @Override
    public ResultPageStruct<FriendlyLink> getFriendlyLinkList(FriendlyLink friendlyLink, PageTo pageParam) throws Exception {
        ResultPageStruct<FriendlyLink> result = new ResultPageStruct<>();
        ParameterDataStruct<FriendlyLink> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(friendlyLink);
        parameterDataStruct.setPageTo(pageParam);
        try {
            // 查询站点列表
            result = friendlyLinkServiceClient.getFriendlyLinkList(parameterDataStruct);
            result.setCode(null);
            result.setMessage(null);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(FriendlyLinkExceptionEnum.FRIENDLYLINK_QUERY_ERROR);

        }

    }


    /**
     * 修改友情链接
     * @param friendlyLink
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct updateFriendlyLink(FriendlyLink friendlyLink) throws Exception {
        if (friendlyLink == null) {
            throw new CustomException(FriendlyLinkExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = friendlyLinkServiceClient.updateFriendlyLink(friendlyLink);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(FriendlyLinkExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(FriendlyLinkExceptionEnum.getEnumByCode(result.getCode()));
        }
    }

    /**
     * 删除友情链接
     * @param friendlyLink
     * @return
     * @throws Exception
     */

    @Override
    public ResultMessageStruct deleteFriendlyLink(FriendlyLink friendlyLink) throws Exception {
        if (friendlyLink == null) {
            throw new CustomException(FriendlyLinkExceptionEnum.PARAMETER_IS_NULL);
        }
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            result = friendlyLinkServiceClient.deleteFriendlyLink(friendlyLink);
            if (result.getCode() == 200) {
                return result;
            } else {
                throw new CustomException(FriendlyLinkExceptionEnum.getEnumByCode(result.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException(FriendlyLinkExceptionEnum.getEnumByCode(result.getCode()));
        }
    }


}
